// -*- Mode: C; c-basic-offset: 8; -*-
//
// Copyright (c) 2012 Andrew Tridgell, All Rights Reserved
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
//  o Redistributions of source code must retain the above copyright
//    notice, this list of conditions and the following disclaimer.
//  o Redistributions in binary form must reproduce the above copyright
//    notice, this list of conditions and the following disclaimer in
//    the documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
// OF THE POSSIBILITY OF SUCH DAMAGE.
//

///
/// @file	crc.c
///
/// crc 16 code
/// see http://www.8052.com/users/bigblack/index.phtml
///

#include <stdarg.h>
#include "radio.h"
#include "crc.h"

// CRC tables
static uint8_t crc_tab1[256] =
    {
        0x00, 0x00, 0x10, 0x21, 0x20, 0x42, 0x30, 0x63, 0x40, 0x84, 0x50, 0xA5, 0x60, 0xC6, 0x70, 0xE7,
        0x81, 0x08, 0x91, 0x29, 0xA1, 0x4A, 0xB1, 0x6B, 0xC1, 0x8C, 0xD1, 0xAD, 0xE1, 0xCE, 0xF1, 0xEF,
        0x12, 0x31, 0x02, 0x10, 0x32, 0x73, 0x22, 0x52, 0x52, 0xB5, 0x42, 0x94, 0x72, 0xF7, 0x62, 0xD6,
        0x93, 0x39, 0x83, 0x18, 0xB3, 0x7B, 0xA3, 0x5A, 0xD3, 0xBD, 0xC3, 0x9C, 0xF3, 0xFF, 0xE3, 0xDE,
        0x24, 0x62, 0x34, 0x43, 0x04, 0x20, 0x14, 0x01, 0x64, 0xE6, 0x74, 0xC7, 0x44, 0xA4, 0x54, 0x85,
        0xA5, 0x6A, 0xB5, 0x4B, 0x85, 0x28, 0x95, 0x09, 0xE5, 0xEE, 0xF5, 0xCF, 0xC5, 0xAC, 0xD5, 0x8D,
        0x36, 0x53, 0x26, 0x72, 0x16, 0x11, 0x06, 0x30, 0x76, 0xD7, 0x66, 0xF6, 0x56, 0x95, 0x46, 0xB4,
        0xB7, 0x5B, 0xA7, 0x7A, 0x97, 0x19, 0x87, 0x38, 0xF7, 0xDF, 0xE7, 0xFE, 0xD7, 0x9D, 0xC7, 0xBC,
        0x48, 0xC4, 0x58, 0xE5, 0x68, 0x86, 0x78, 0xA7, 0x08, 0x40, 0x18, 0x61, 0x28, 0x02, 0x38, 0x23,
        0xC9, 0xCC, 0xD9, 0xED, 0xE9, 0x8E, 0xF9, 0xAF, 0x89, 0x48, 0x99, 0x69, 0xA9, 0x0A, 0xB9, 0x2B,
        0x5A, 0xF5, 0x4A, 0xD4, 0x7A, 0xB7, 0x6A, 0x96, 0x1A, 0x71, 0x0A, 0x50, 0x3A, 0x33, 0x2A, 0x12,
        0xDB, 0xFD, 0xCB, 0xDC, 0xFB, 0xBF, 0xEB, 0x9E, 0x9B, 0x79, 0x8B, 0x58, 0xBB, 0x3B, 0xAB, 0x1A,
        0x6C, 0xA6, 0x7C, 0x87, 0x4C, 0xE4, 0x5C, 0xC5, 0x2C, 0x22, 0x3C, 0x03, 0x0C, 0x60, 0x1C, 0x41,
        0xED, 0xAE, 0xFD, 0x8F, 0xCD, 0xEC, 0xDD, 0xCD, 0xAD, 0x2A, 0xBD, 0x0B, 0x8D, 0x68, 0x9D, 0x49,
        0x7E, 0x97, 0x6E, 0xB6, 0x5E, 0xD5, 0x4E, 0xF4, 0x3E, 0x13, 0x2E, 0x32, 0x1E, 0x51, 0x0E, 0x70,
        0xFF, 0x9F, 0xEF, 0xBE, 0xDF, 0xDD, 0xCF, 0xFC, 0xBF, 0x1B, 0xAF, 0x3A, 0x9F, 0x59, 0x8F, 0x78};

static uint8_t crc_tab2[256] =
    {
        0x91, 0x88, 0x81, 0xA9, 0xB1, 0xCA, 0xA1, 0xEB, 0xD1, 0x0C, 0xC1, 0x2D, 0xF1, 0x4E, 0xE1, 0x6F,
        0x10, 0x80, 0x00, 0xA1, 0x30, 0xC2, 0x20, 0xE3, 0x50, 0x04, 0x40, 0x25, 0x70, 0x46, 0x60, 0x67,
        0x83, 0xB9, 0x93, 0x98, 0xA3, 0xFB, 0xB3, 0xDA, 0xC3, 0x3D, 0xD3, 0x1C, 0xE3, 0x7F, 0xF3, 0x5E,
        0x02, 0xB1, 0x12, 0x90, 0x22, 0xF3, 0x32, 0xD2, 0x42, 0x35, 0x52, 0x14, 0x62, 0x77, 0x72, 0x56,
        0xB5, 0xEA, 0xA5, 0xCB, 0x95, 0xA8, 0x85, 0x89, 0xF5, 0x6E, 0xE5, 0x4F, 0xD5, 0x2C, 0xC5, 0x0D,
        0x34, 0xE2, 0x24, 0xC3, 0x14, 0xA0, 0x04, 0x81, 0x74, 0x66, 0x64, 0x47, 0x54, 0x24, 0x44, 0x05,
        0xA7, 0xDB, 0xB7, 0xFA, 0x87, 0x99, 0x97, 0xB8, 0xE7, 0x5F, 0xF7, 0x7E, 0xC7, 0x1D, 0xD7, 0x3C,
        0x26, 0xD3, 0x36, 0xF2, 0x06, 0x91, 0x16, 0xB0, 0x66, 0x57, 0x76, 0x76, 0x46, 0x15, 0x56, 0x34,
        0xD9, 0x4C, 0xC9, 0x6D, 0xF9, 0x0E, 0xE9, 0x2F, 0x99, 0xC8, 0x89, 0xE9, 0xB9, 0x8A, 0xA9, 0xAB,
        0x58, 0x44, 0x48, 0x65, 0x78, 0x06, 0x68, 0x27, 0x18, 0xC0, 0x08, 0xE1, 0x38, 0x82, 0x28, 0xA3,
        0xCB, 0x7D, 0xDB, 0x5C, 0xEB, 0x3F, 0xFB, 0x1E, 0x8B, 0xF9, 0x9B, 0xD8, 0xAB, 0xBB, 0xBB, 0x9A,
        0x4A, 0x75, 0x5A, 0x54, 0x6A, 0x37, 0x7A, 0x16, 0x0A, 0xF1, 0x1A, 0xD0, 0x2A, 0xB3, 0x3A, 0x92,
        0xFD, 0x2E, 0xED, 0x0F, 0xDD, 0x6C, 0xCD, 0x4D, 0xBD, 0xAA, 0xAD, 0x8B, 0x9D, 0xE8, 0x8D, 0xC9,
        0x7C, 0x26, 0x6C, 0x07, 0x5C, 0x64, 0x4C, 0x45, 0x3C, 0xA2, 0x2C, 0x83, 0x1C, 0xE0, 0x0C, 0xC1,
        0xEF, 0x1F, 0xFF, 0x3E, 0xCF, 0x5D, 0xDF, 0x7C, 0xAF, 0x9B, 0xBF, 0xBA, 0x8F, 0xD9, 0x9F, 0xF8,
        0x6E, 0x17, 0x7E, 0x36, 0x4E, 0x55, 0x5E, 0x74, 0x2E, 0x93, 0x3E, 0xB2, 0x0E, 0xD1, 0x1E, 0xF0};

// calculate the CRC16 of a buffer
// this costs about 2.2 microseconds per byte
uint16_t
crc16(uint8_t n, uint8_t *buf)
{
    register uint8_t k;
    register uint8_t high, low;

    high = low = 0;

    while (n--)
    {
        register uint8_t b = *buf++;
        k = high << 1;
        if (high & 0x80)
        {
            high = low ^ crc_tab2[k++];
            low = b ^ crc_tab2[k];
        }
        else
        {
            high = low ^ crc_tab1[k++];
            low = b ^ crc_tab1[k];
        }
    }
    return (((uint16_t)high) << 8) | low;
}
